package experiment_1;

/**
 * Created by Administrator on 2016/4/10.
 */
import java.lang.*;
import java.io.*;
import experiment_1.*;

public class Main
{
    public static void main(String[] args)
    {
        while(true)
        {
            Menu.paint();
            int ctrlTag = Console.readInt();
            switch(ctrlTag)
            {
                case 1:
                {
                    Menu.action1();
                    break;
                }

                case 2:
                {
                    Menu.action2();
                    break;
                }

                case 3:
                {
                    Menu.action3();
                    break;
                }

                case 4:
                {
                    Menu.action4();
                    break;
                }

                case 0:
                {
                    System.exit(0);
                    break;
                }
            }
        }
    }
}

class Student
{
    private int number;
    private String name;
    private int english;
    private int math;
    private int computer;
    private int average;
    private int total;

    public Student()
    {
    }

    public Student(int number, String name, int english, int math, int computer)
    {
        this.number = number;
        this.name = name;
        this.english = english;
        this.math = math;
        this.computer = computer;
        total = english + math + computer;
        average = total/3;
    }

    public int getNumber()
    {
        return number;
    }

    public String getName()
    {
        return name;
    }

    public int getEnglish()
    {
        return english;
    }

    public int getMath()
    {
        return math;
    }

    public int getComputer()
    {
        return computer;
    }

    public int getAverage()
    {
        return average;
    }

    public int getTotal()
    {
        return total;
    }
}

class StudentSet
{
    private static final int maxLen = 1000;
    private static int len = 0;
    private static Student[] data = new Student[maxLen];

    public static int getMaxLen()
    {
        return maxLen;
    }

    public static int getLen()
    {
        return len;
    }

    public static Student getData(int index)
    {
        return data[index];
    }

    public static int addData(Student newData)
    {
        if (len < maxLen)
        {
            data[len] = newData;
            len++;
            return 0;
        } else
        {
            return -1;
        }
    }

    public static int search(int number)
    {
        for(int i = 0; i < len; i++)
        {
            if (data[i].getNumber() == number)
            {
                return i;
            }
        }
        return -1;
    }

    public static int delete(int number)
    {
        for (int i = 0; i < len; i++)
        {
            if (data[i].getNumber() == number)
            {
                for (int j = i + 1; j < len; j++)
                {
                    data[j-1] = data[j];
                }
                len--;
                return 0;
            }
        }
        return -1;
    }
}

class Console
{
    public static int readInt()
    {
        int result = 0;
        try{
            String temp =new BufferedReader(new InputStreamReader(System.in)).readLine();
            result = Integer.parseInt(temp);
        } catch (Exception e)
        {
            System.out.println("Error: "+e);
        }
        return result;
    }

    public static String readSting()
    {
        String result = null;
        try{
            result = new BufferedReader(new InputStreamReader(System.in)).readLine();
        } catch (Exception e)
        {
            System.out.println("Error: "+e);
        }
        return result;
    }
}






class Menu
{
    public static void paint()
    {
        clrscr();
        for (int i = 0; i < 80; i++)
        {
            System.out.print('=');
        }
        System.out.println();

        System.out.println("                             学生成绩管理系统                 ");
        System.out.println();
        System.out.println("               1 输入记录                       2 输出所有记录");
        System.out.println("               3 删除记录                       4 查找记录");
        System.out.println("               0 退出");

        for (int i = 0; i < 80; i++)
        {
            System.out.print('=');
        }
        System.out.println();
        System.out.print("请输入命令：");
    }

    public static int action1()
    {
        while(true)
        {
            clrscr();
            System.out.print("待输入记录的学号（输入“-1”退出）：");

            int number = Console.readInt();
            if (number != -1)
            {
                System.out.print("  *姓名：");
                String name = Console.readSting();
                System.out.print("  *英语：");
                int english = Console.readInt();
                System.out.print("  *数学：");
                int math = Console.readInt();
                System.out.print("  *计算机：");
                int computer = Console.readInt();
                Student newData = new Student(number, name, english, math, computer);
                if (StudentSet.addData(newData) == -1)
                {
                    System.out.println("数据溢出！");
                    return -2;
                }
            } else
            {
                return -1;
            }
        }
    }

    public static void action2()
    {
        clrscr();
        if (StudentSet.getLen() == 0)
        {
            System.out.println("没有记录！");
        } else
        {
            System.out.println("         数学|英语|计算机|平均|总计");
            for (int i = 0; i < StudentSet.getLen(); i++)
            {
                System.out.println("学号："+StudentSet.getData(i).getNumber()+"  姓名："+StudentSet.getData(i).getName());
                System.out.println("      "+StudentSet.getData(i).getMath()+"    "+StudentSet.getData(i).getEnglish()+
                        "    "+StudentSet.getData(i).getComputer()+"    "+StudentSet.getData(i).getAverage()+"    "+StudentSet.getData(i).getTotal());
            }
        }
    }

    public static void action3()
    {
        while(true)
        {
            clrscr();
            System.out.print("请输入要删除记录的学号（输入“-1”退出）：");
            int number = Console.readInt();
            if (number != -1)
            {
                if(StudentSet.delete(number) == -1)
                {
                    System.out.println("没有找到相应的记录！");
                    return;
                } else
                {
                    System.out.println("删除记录成功！");
                }
            } else
            {
                return;
            }
        }
    }

    public static void action4()
    {
        clrscr();
        System.out.print("请输入要查找记录的学号：");
        int number = Console.readInt();
        int i;
        if ((i = StudentSet.search(number)) != -1)
        {
            clrscr();
            System.out.println("         数学|英语|计算机|平均|总计");
            System.out.println("学号："+StudentSet.getData(i).getNumber()+"  姓名："+StudentSet.getData(i).getName());
            System.out.println("      "+StudentSet.getData(i).getMath()+"    "+StudentSet.getData(i).getEnglish()+
                    "    "+StudentSet.getData(i).getComputer()+"    "+StudentSet.getData(i).getAverage()+"    "+StudentSet.getData(i).getTotal());
        } else
        {
            System.out.println("没有找到相应的记录！");
        }
    }



    public static void clrscr()
    { //temp function
        System.out.println();
        System.out.println();
    }
}
